home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / lib / lsb / init-functions
Encoding:
Text File  |  2006-11-19  |  7.8 KB  |  301 lines

  1. # /lib/lsb/init-functions for Debian -*- shell-script -*-
  2. #
  3. #Copyright (c) 2002-06 Chris Lawrence
  4. #All rights reserved.
  5. #
  6. #Redistribution and use in source and binary forms, with or without
  7. #modification, are permitted provided that the following conditions
  8. #are met:
  9. #1. Redistributions of source code must retain the above copyright
  10. #   notice, this list of conditions and the following disclaimer.
  11. #2. Redistributions in binary form must reproduce the above copyright
  12. #   notice, this list of conditions and the following disclaimer in the
  13. #   documentation and/or other materials provided with the distribution.
  14. #3. Neither the name of the author nor the names of other contributors
  15. #   may be used to endorse or promote products derived from this software
  16. #   without specific prior written permission.
  17. #
  18. #THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. #ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. #IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. #ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. #FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. #DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. #OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. #HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. #LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. #OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. #SUCH DAMAGE.
  29.  
  30. start_daemon () {
  31.     local force nice pidfile exec i
  32.     force=0
  33.     nice=0
  34.     pidfile=/dev/null
  35.  
  36.     OPTIND=1
  37.     while getopts fn:p: opt ; do
  38.         case "$opt" in
  39.             f)  force=1;;
  40.             n)  nice="$OPTARG";;
  41.             p)  pidfile="$OPTARG";;
  42.         esac
  43.     done
  44.     
  45.     shift $(($OPTIND - 1))
  46.     [ "$1" = '--' ] && shift
  47.  
  48.     exec="$1"; shift
  49.  
  50.     if [ $force = 1 ]; then
  51.         eval /sbin/start-stop-daemon --start --nicelevel $nice --quiet --startas $exec --pidfile /dev/null --oknodo -- "$@"
  52.     elif [ $pidfile ]; then
  53.         eval /sbin/start-stop-daemon --start --nicelevel $nice --quiet --exec $exec --oknodo --pidfile "$pidfile" -- "$@"
  54.     else
  55.         eval /sbin/start-stop-daemon --start --nicelevel $nice --quiet --exec $exec --oknodo -- "$@"
  56.     fi
  57. }
  58.  
  59. pidofproc () {
  60.     local pidfile line i pids= status specified pid
  61.     pidfile=
  62.     specified=
  63.     
  64.     OPTIND=1
  65.     while getopts p: opt ; do
  66.         case "$opt" in
  67.             p)  pidfile="$OPTARG"; specified=1;;
  68.         esac
  69.     done
  70.     shift $(($OPTIND - 1))
  71.  
  72.     if [ -z "${pidfile:-}" ]; then
  73.         pidfile=/var/run/${1##*/}.pid
  74.     fi
  75.  
  76.     if [ -f "$pidfile" ]; then
  77.         read pid < "$pidfile"
  78.         if [ -n "${pid:-}" ]; then
  79.             if $(kill -0 "${pid:-}" 2> /dev/null); then
  80.                 echo "$pid"
  81.                 return 0
  82.             else
  83.                 return 1 # program is dead and /var/run pid file exists
  84.             fi
  85.         fi
  86.     fi
  87.     if [ -x /bin/pidof -a ! "$specified" ]; then
  88.         /bin/pidof -o %PPID $1
  89.         status="$?"
  90.         [ "$status" = 1 ] && return 3 # program is not running
  91.         return 0
  92.     fi
  93.     return 4 # program or service is unknown
  94. }
  95.  
  96. # start-stop-daemon uses the same algorithm as "pidofproc" above.
  97. killproc () {
  98.     local pidfile sig status base i name_param
  99.     pidfile=
  100.     name_param=
  101.  
  102.     OPTIND=1
  103.     while getopts p: opt ; do
  104.         case "$opt" in
  105.             p)  pidfile="$OPTARG";;
  106.         esac
  107.     done
  108.     shift $(($OPTIND - 1))
  109.  
  110.     base=${1##*/}
  111.     if [ ! $pidfile ]; then
  112.         pidfile=/var/run/$base.pid
  113.         name_param="--name $base"
  114.     fi
  115.  
  116.     sig=$(echo ${2:-} | sed -e 's/^-\(.*\)/\1/')
  117.     sig=$(echo $sig | sed -e 's/^SIG\(.*\)/\1/')
  118.     if [ -n "$sig" ]; then
  119.         /sbin/start-stop-daemon --stop --pidfile "$pidfile" --signal "$sig" --quiet $name_param
  120.     else
  121.         /sbin/start-stop-daemon --stop --pidfile "$pidfile" --retry 5 --quiet --oknodo $name_param
  122.     fi
  123.     status="$?"
  124.     if [ "$status" = 1 ]; then
  125.         [ -n "$sig" ] && return 0
  126.         return 3 # program is not running
  127.     fi
  128.     return 0
  129.  
  130.     rm -f "$pidfile"
  131. }
  132.  
  133. log_use_fancy_output () {
  134.     TPUT=/usr/bin/tput
  135.     EXPR=/usr/bin/expr
  136.     if [ "x$TERM" != "xdumb" ] && [ -x $TPUT ] && [ -x $EXPR ] && $TPUT hpa 60 >/dev/null 2>&1 && $TPUT setaf 1 >/dev/null 2>&1; then
  137.         [ -z $FANCYTTY ] && FANCYTTY=1 || true
  138.     else
  139.         FANCYTTY=0
  140.     fi
  141.     case "$FANCYTTY" in
  142.         1|Y|yes|true)   true;;
  143.         *)              false;;
  144.     esac
  145. }
  146.  
  147. log_success_msg () {
  148.     echo "$@"
  149. }
  150.  
  151. log_failure_msg () {
  152.     if log_use_fancy_output; then
  153.         RED=`$TPUT setaf 1`
  154.         NORMAL=`$TPUT op`
  155.         echo "$RED*$NORMAL $@"
  156.     else
  157.         echo "$@"
  158.     fi
  159. }
  160.  
  161. log_warning_msg () {
  162.     if log_use_fancy_output; then
  163.         YELLOW=`$TPUT setaf 3`
  164.         NORMAL=`$TPUT op`
  165.         echo "$YELLOW*$NORMAL $@"
  166.     else
  167.         echo "$@"
  168.     fi
  169. }
  170.  
  171. #
  172. # NON-LSB HELPER FUNCTIONS
  173. #
  174. # int get_lsb_header_val (char *scriptpathname, char *key)
  175. get_lsb_header_val () {
  176.         if [ ! -f "$1" ] || [ -z "${2:-}" ]; then
  177.                 return 1
  178.         fi
  179.         LSB_S="### BEGIN INIT INFO"
  180.         LSB_E="### END INIT INFO"
  181.         sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
  182. }
  183.  
  184. # int log_begin_message (char *message)
  185. log_begin_msg () {
  186.     if [ -z "${1:-}" ]; then
  187.         return 1
  188.     fi
  189.     echo -n "$@"
  190. }
  191.  
  192. # Sample usage:
  193. # log_daemon_msg "Starting GNOME Login Manager" "gdm"
  194. #
  195. # On Debian, would output "Starting GNOME Login Manager: gdm"
  196. # On Ubuntu, would output " * Starting GNOME Login Manager..."
  197. #
  198. # If the second argument is omitted, logging suitable for use with
  199. # log_progress_msg() is used:
  200. #
  201. # log_daemon_msg "Starting remote filesystem services"
  202. #
  203. # On Debian, would output "Starting remote filesystem services:"
  204. # On Ubuntu, would output " * Starting remote filesystem services..."
  205.  
  206. log_daemon_msg () {
  207.     if [ -z "${1:-}" ]; then
  208.         return 1
  209.     fi
  210.  
  211.     if [ -z "${2:-}" ]; then
  212.         echo -n "$1:"
  213.         return
  214.     fi
  215.     
  216.     echo -n "$1: $2"
  217. }
  218.  
  219. # #319739
  220. #
  221. # Per policy docs:
  222. #
  223. #     log_daemon_msg "Starting remote file system services"
  224. #     log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd
  225. #     log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd
  226. #     log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd
  227. #     log_end_msg 0
  228. #
  229. # You could also do something fancy with log_end_msg here based on the
  230. # return values of start-stop-daemon; this is left as an exercise for
  231. # the reader...
  232. #
  233. # On Ubuntu, one would expect log_progress_msg to be a no-op.
  234. log_progress_msg () {
  235.     if [ -z "${1:-}" ]; then
  236.         return 1
  237.     fi
  238.     echo -n " $@"
  239. }
  240.  
  241.  
  242. # int log_end_message (int exitstatus)
  243. log_end_msg () {
  244.     # If no arguments were passed, return
  245.     [ -z "${1:-}" ] && return 1
  246.  
  247.     # Only do the fancy stuff if we have an appropriate terminal
  248.     # and if /usr is already mounted
  249.     if log_use_fancy_output; then
  250.         RED=`$TPUT setaf 1`
  251.         NORMAL=`$TPUT op`
  252.         if [ $1 -eq 0 ]; then
  253.             echo "."
  254.         else
  255.             /bin/echo -e " ${RED}failed!${NORMAL}"
  256.         fi
  257.     else
  258.     if [ $1 -eq 0 ]; then
  259.             echo "."
  260.         else
  261.             echo " failed!"
  262.         fi
  263.     fi
  264.     return $1
  265. }
  266.  
  267. log_action_msg () {
  268.     echo "$@."
  269. }
  270.  
  271. log_action_begin_msg () {
  272.     echo -n "$@..."
  273. }
  274.  
  275. log_action_cont_msg () {
  276.     echo -n "$@..."
  277. }
  278.  
  279. log_action_end_msg () {
  280.     if [ -z "${2:-}" ]; then
  281.         end="."
  282.     else
  283.         end=" ($2)."
  284.     fi
  285.  
  286.     if [ $1 -eq 0 ]; then
  287.         echo "done${end}"
  288.     else
  289.         if log_use_fancy_output; then
  290.             RED=`$TPUT setaf 1`
  291.             NORMAL=`$TPUT op`
  292.             /bin/echo -e "${RED}failed${end}${NORMAL}"
  293.         else
  294.             echo "failed${end}"
  295.         fi
  296.     fi
  297. }
  298.  
  299. FANCYTTY=
  300. [ -e /etc/lsb-base-logging.sh ] && . /etc/lsb-base-logging.sh || true
  301.